package solutions.leetcode.normal

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/make-number-of-distinct-characters-equal/">2531. 使字符串总不同字符的数目相等</a>
 */
private class Solution2531 {
    fun isItPossible(word1: String, word2: String): Boolean {
        val c1 = IntArray(26)
        val c2 = IntArray(26)
        for (item in word1) {
            c1[item - 'a']++
        }
        for (item in word2) {
            c2[item - 'a']++
        }
        for (i in 0 until 26) {
            for (j in 0 until 26) {
                if (c1[i] > 0 && c2[j] > 0) {
                    c1[i]--
                    c2[i]++
                    c1[j]++
                    c2[j]--
                    if (checkOk(c1, c2)) {
                        return true
                    }
                    c2[j]++
                    c1[j]--
                    c2[i]--
                    c1[i]++
                }
            }
        }
        return false
    }

    private fun checkOk(c1: IntArray, c2: IntArray): Boolean {
        return c1.count { it > 0 } == c2.count { it > 0 }
    }
}

fun main() {
    println(Solution2531().isItPossible(
        "ac",
        "b"
    ))
}